﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;
namespace OT.Areas.Admin.Controllers
{
    public class QuestionTypeController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        #endregion

        #region Index
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex)
        {
            List<QuestionTypes> Que = ((from c in OTDb.QuestionTypes
                                        select new QuestionTypes
                                        {
                                            QuestionTypeID = c.QuestionTypeID,
                                            QuestionType1 = c.QuestionType1,
                                            CreatedDate = (c.CreatedDate.HasValue) ? c.CreatedDate.Value : DateTime.MinValue,
                                            UpdatedDate = (c.UpdatedDate.HasValue) ? c.UpdatedDate.Value : DateTime.MinValue,
                                        }).OrderBy(o => o.QuestionType1).ToList<QuestionTypes>()); ;
            if (!string.IsNullOrEmpty(searchString))
                Que = Que.Where(r => r.QuestionType1.ToLower().Contains(searchString.ToLower())).ToList();
            int recount =  Que.Count;
            Que = Que.Skip<QuestionTypes>(pgindex * (pageNumber - 1))
                .Take<QuestionTypes>(pgindex).ToList<QuestionTypes>();
            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = Que,
                RecordCount = recount
            });
        }
        #endregion

        #region Create
        public ActionResult Create()
        {
            return PartialView();
        }

        [HttpPost]
        public string Create(String QuestionType)
        {
            string result = "";
            QuestionType Q = null;
            try
            {
                if (ModelState.IsValid)
                {
                    Q = new QuestionType();
                    Q.QuestionType1 = QuestionType;
                    Q.CreatedDate = DateTime.Now;
                    Q.UpdatedDate = DateTime.Now;
                    OTDb.QuestionTypes.Add(Q);
                    OTDb.SaveChanges();
                    result = "Sucess";
                }
                else
                {
                    var Citys = OTDb.QuestionTypes.ToList();
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Edit
        public ActionResult Edit(int QuestionTypeID)
        {
            QuestionTypes que = null;
            try
            {
                var categry = OTDb.QuestionTypes.Where(r => r.QuestionTypeID == QuestionTypeID).SingleOrDefault();
                if (categry != null)
                {
                    que = new QuestionTypes();
                    que.QuestionTypeID = categry.QuestionTypeID;
                    que.QuestionType1 = categry.QuestionType1;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(que);
        }
        [HttpPost]
        public string Edit(int QuestionTypeID, String QuestionType)
        {
            string result = "";
            try
            {
                if (ModelState.IsValid)
                {
                    var que = OTDb.QuestionTypes.Where(r => r.QuestionTypeID == QuestionTypeID).SingleOrDefault();
                    if (que != null)
                    {
                        que.QuestionType1 = QuestionType;
                        que.QuestionTypeID = QuestionTypeID;
                        que.UpdatedDate = DateTime.Now;
                        OTDb.SaveChanges();
                        result = "Sucess";
                    }
                }
                else
                {
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Delete
        public ActionResult Delete(int QuestionTypeID)
        {
            QuestionTypes que = null;
            try
            {
                var categry = OTDb.QuestionTypes.Where(r => r.QuestionTypeID == QuestionTypeID).SingleOrDefault();
                if (categry != null)
                {
                    que = new QuestionTypes();
                    que.QuestionTypeID = categry.QuestionTypeID;
                    que.QuestionType1 = categry.QuestionType1;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(que);
        }

        [HttpPost]
        public string Delete(int QuestionTypeID, string IsDelete)
        {
            string result = "";
            try
            {
                var cat = OTDb.QuestionTypes.Where(r => r.QuestionTypeID == QuestionTypeID).SingleOrDefault();
                if (cat != null)
                {
                    OTDb.QuestionTypes.Remove(cat);
                    OTDb.SaveChanges();
                    result = "Success";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion
    }
}

